﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace ExcelBlockLib
{
    public static class ExcelHelper
    {
        /// <summary>
        /// 将单条 block 转换为 Tuple<材质类别, 唯一键>
        /// </summary>
        public static Tuple<string, string> BlockToDeleteTarget(Dictionary<string, string> block)
        {
            if (block == null || !block.ContainsKey("材质类别"))
                return null;

            string category = block["材质类别"];
            string key = ExcelBlockService.GetUniqueKey(block);

            return string.IsNullOrWhiteSpace(key) ? null : Tuple.Create(category, key);
        }

        /// <summary>
        /// 批量转换 block 列表为逻辑删除目标
        /// </summary>
        public static List<Tuple<string, string>> BlocksToDeleteTargets(List<Dictionary<string, string>> blocks)
        {
            return blocks
                .Select(BlockToDeleteTarget)
                .Where(t => t != null)
                .ToList();
        }

        /// <summary>
        /// 过滤掉被逻辑删除的记录（__deleted__ == "1"）
        /// </summary>
        public static List<Dictionary<string, string>> FilterActiveBlocks(List<Dictionary<string, string>> blocks)
        {
            return blocks
                .Where(b => !b.ContainsKey("__deleted__") || b["__deleted__"] != "1")
                .ToList();
        }
    }
}
